
function BizProcCoordinator(pageId, baseUrl)
{
	this.pageId = pageId;
	this.baseUrl = baseUrl + '?';
	this.bizProcs = new Array();
}

BizProcCoordinator.prototype.addBizProc = function(bizProc)
{
	this.bizProcs.push(bizProc);
}

BizProcCoordinator.prototype.triggerEvent = function(bizProc, eventInfo)
{
	var doc = nitobi.xml.createXmlDoc('<ClientEvent />');
	var docEl = doc.documentElement;
	docEl.setAttribute('bizProcId', bizProc==null?'':bizProc.id);
	eventInfo.addState(docEl);
	for(var p = 0; p < this.bizProcs.length; p++)
		this.processBizProc(doc, docEl, this.bizProcs[p]);
	
	this.sendRequest(doc);
}

BizProcCoordinator.prototype.processBizProc = function(doc, docEl, bizProc)
{
	var bizProcEl = doc.createElement('BizProc');
	bizProc.getState(bizProcEl);
	docEl.appendChild(bizProcEl);
}

BizProcCoordinator.prototype.sendRequest = function(doc)
{
	var xhr = new nitobi.ajax.HttpRequest();
	xhr.handler = this.baseUrl + 'pi=' + this.pageId + '&RequestType=' + doc.documentElement.getAttribute('event');
	xhr.async = false;
	xhr.responseType = 'xml';
	var webRespDoc = xhr.post(nitobi.xml.serialize(doc.documentElement));
	
	this.processWebResponse(webRespDoc);
}

BizProcCoordinator.prototype.processWebResponse = function(doc)
{
	this.tempBizProcs = new Array();
	for(var pos = 0; pos < this.bizProcs.length; pos++)
		this.bizProcs[pos].processControlStates(doc);
}

function EventInfo(ctlId, eventId, val)
{
	this.ctlId = ctlId;
	this.eventId = eventId;
	this.val = val;
}

EventInfo.prototype.addState = function(eventEl)
{
	eventEl.setAttribute('event', this.eventId);
	eventEl.setAttribute('id', this.ctlId);
	eventEl.setAttribute('value', this.val);	
}
function ClientBizProc(id)
{
	this.id = id;
	this.ClientControls = new Array();
	bizProcManager.addBizProc(this);
}

ClientBizProc.prototype.addClientControl = function(ctl)
{
	for(var pos = 0; pos < this.ClientControls.length;pos++)
		if(this.ClientControls[pos].id == ctl.id)
		{
			this.ClientControls[pos] = ctl;
			return;
		}
	this.ClientControls[this.ClientControls.length] = ctl;
}

ClientBizProc.prototype.triggerEvent = function(ctlId, eventId, val)
{
	bizProcManager.triggerEvent(this, new EventInfo(ctlId, eventId, val));
}

ClientBizProc.prototype.getState = function(bizProcEl)
{
	bizProcEl.setAttribute('id', this.id);
	this.insertControlStates(bizProcEl.ownerDocument, bizProcEl);
}

ClientBizProc.prototype.processControlStates = function(doc)
{
	for(var pos = 0; pos < this.ClientControls.length; pos++)
	{
		var clientCtl = this.ClientControls[pos];
		var clientId = clientCtl.id;
		if(clientCtl.dhtmlObj != null)
		{
			if(clientId == null || clientId == '')
				clientId = clientCtl.dhtmlObj.id;
			var stateEl = doc.selectSingleNode("//ControlStates/State[@id = '" + clientId + "']");
			if(stateEl != null && clientCtl.setState != null)
				clientCtl.setState(stateEl);
		}
	}	
}

ClientBizProc.prototype.insertControlStates = function(doc, bizProcEl)
{
	var states = doc.createElement('ControlStates');
	bizProcEl.appendChild(states);
	for(var pos = 0; pos < this.ClientControls.length; pos++)
	{
		var clientCtl = this.ClientControls[pos];
		if(clientCtl.getState != null && clientCtl.dhtmlObj != null)
		{
			var stateEl = doc.createElement('State');
			var clientId = clientCtl.id;
			if(clientId == null || clientId == '')
				clientId = clientCtl.dhtmlObj.id;
			stateEl.setAttribute('id', clientId);
			this.ClientControls[pos].getState(stateEl);
			states.appendChild(stateEl);
		}
	}
}

function ClientControl(dhtmlObj, setStateFun, getStateFun, showBizObjFun, updateBizObjFun)
{
	this.dhtmlObj = dhtmlObj;
	this.setState = setStateFun;
	this.getState = getStateFun;
	this.showBizObj = showBizObjFun;
	this.updateBizObj = updateBizObjFun;
}


function DivResizer(div){
	this.div=div;
	this.box=document.getElementById("ebagridresizebox_");
	if(this.box==null){
	this.box=document.createElement("div");
	this.box.id="ebagridresizebox_";
	document.body.appendChild(this.box);
	this.box.className="ntbcolumnresizeline";
	}
}

DivResizer.prototype.startResize=function(div,evt){
this.div=div;
var _2cb=null;
var x,y;
var _2ce=nitobi.html.getEventCoords(evt);
x=_2ce.x;
y=_2ce.y;
this.x=x;
this.y=y;
var w=div.pixelWidth;
var h=div.pixelHeight;
var L=div.offsetLeft;
var T=div.offsetTop;
this.resizeW=(Math.abs((x-L)-w)<3)||((Math.abs((y-T)-h)<16)&&(Math.abs((x-L)-w)<16));
this.resizeH=(Math.abs((y-T)-h)<3)||((Math.abs((y-T)-h)<16)&&(Math.abs((x-L)-w)<16));
if(this.resizeW||this.resizeH){
this.box.style.cursor=(this.resizeW&&this.resizeH)?"NW-Resize":(this.resizeW)?"W-Resize":"N-Resize";
this.box.style.visibility="visible";
this.box.style.width=(x-L)+"px";
this.box.style.height=(y-T)+"px";
var _2d3=nitobi.drawing.align.SAMEWIDTH|nitobi.drawing.align.SAMEHEIGHT|nitobi.drawing.align.ALIGNTOP|nitobi.drawing.align.ALIGNLEFT;
nitobi.drawing.align(this.box,this.div,_2d3,0,0,0,0,false);
this.dd=new nitobi.ui.DragDrop(this.box,false,false);
this.dd.onDragStop.subscribe(this.endResize,this);
this.dd.onMouseMove.subscribe(this.resize,this);
this.dd.startDrag(evt);
}
};

DivResizer.prototype.resize=function(){
var x=this.dd.x;
var y=this.dd.y;
var L=this.div.offsetLeft;
var T=this.div.offsetTop;
this.box.style.visibility="visible";
if(this.resizeW){
this.box.style.width=(x-L)+"px";
}
if(this.resizeH){
this.box.style.height=(y-T)+"px";
}
};

DivResizer.prototype.endResize=function(){
var x=this.dd.x;
var y=this.dd.y;
this.box.style.visibility="hidden";
var _2da=this.div.pixelWidth;
var _2db=this.div.pixelHeight;
var _2dc=parseInt(_2da)+((this.resizeW)?x-this.x:0);
var _2dd=parseInt(_2db)+((this.resizeH)?y-this.y:0);
if(isNaN(_2dc)||isNaN(_2dd)){
return;
}
if(_2dc>20&&_2dd>20){
this.div.style.width = _2dc + "px";
this.div.style.height = _2dd + "px";
}
};

DivResizer.prototype.dispose=function(){
this.grid=null;
};

function parseUsaDate(date){
debugger;
	return new Date(date.replace(/^(\d+).(\d+).(\d+).(.*)$/,"$3/$1/$2 $4"));
};

function toUsaDate(date){
	if(nitobi.base.DateMath.invalid(date)){
		return "";
	}
	var pz=nitobi.lang.padZeros;
	return pz(date.getMonth()+1)+"-"+pz(date.getDate())+"-"+date.getFullYear();
};
